<?php
/**
* @property CI_Loader $load
* @property CI_Form_validation $form_validation
* @property CI_Input $input
* @property CI_Email $email
* @property CI_DB_active_record $db
*/
class Shiftconnection extends CI_Model
{
    /**
     *
     * @param <string> $newaddress
     * @param <string> $phoneno
     * @param <string> $causeshift
     */
    function insert($newaddress,$phoneno,$causeshift)
    {

        $this->db->set('newaddress',$newaddress);
        $this->db->set('applydate','NOW()',false);
        $this->db->set('status','Recent');
        $this->db->set('message','Your application is sent to Divisional Engineer');
        $this->db->set('causeshift',$causeshift);
        $this->db->set('phoneno',$phoneno);
        $this->db->insert('shiftconnection');

        return $this->db->insert_id();
    }
    function get_Detail($appid)
    {
         $query = $this->db->query("SELECT
                                    shiftconnection.appid,
                                    shiftconnection.newaddress,
                                    shiftconnection.causeshift,
                                    telephone.phoneno,
                                    telephone.address
                                    FROM
                                    shiftconnection
                                    Inner Join telephone ON shiftconnection.phoneno = telephone.phoneno
                                    WHERE
                                    shiftconnection.appid  = $appid");
        return $query->row();
    }
     function get_result($appid)
    {
         $query = $this->db->query("SELECT
                                    shiftconnection.appid,
                                    shiftconnection.newaddress,
                                    shiftconnection.causeshift,
                                    shiftconnection.result,
                                    telephone.phoneno,
                                    telephone.address
                                    FROM
                                    shiftconnection
                                    Inner Join telephone ON shiftconnection.phoneno = telephone.phoneno
                                    WHERE
                                    shiftconnection.appid  = $appid");
        return $query->row();
    }

    function shift_message($appid)
    {
         $query = $this->db->query("SELECT
                                    shiftconnection.message,
                                    shiftconnection.applydate
                                    FROM
                                    shiftconnection
                                    WHERE
                                    shiftconnection.appid  = $appid");
        return $query->row_array();
    
    }
    function get_phone($appid)
    {

        $this->db->where('appid',$appid);
        $query=$this->db->get("shiftconnection");
        $phone = $query->row();
        
        return $phone->phoneno;
    }
    function get_newaddress($appid)
    {
        $this->db->where('appid',$appid);
        $query=$this->db->get("shiftconnection");
        $new = $query->row();

        return $new->newaddress;
    }
    function already_apply($phoneno)
    {
        $this->db->where('accomplishdate is NULL');
        $this->db->where('phoneno',$phoneno);
        $query = $this->db->get('shiftconnection');

        return $query->num_rows();
    }
    function recently_arrived($id,$status)
    {
          $query = $this->db->query("SELECT
                                shiftconnection.appid,
                                shiftconnection.applydate,
                                shiftconnection.updatedate,
                                shiftconnection.result,
                                shiftconnection.accomplishdate
                                FROM
                                shifthandle
                                Inner Join shiftconnection ON shifthandle.appid = shiftconnection.appid
                                WHERE
                                empid = '$id' and status='$status'");
        return $query;
    }
    function update_status($id,$status)
    {

        $this->db->set('status', $status);
        $this->db->where("appid",$id);
        $this->db->update('shiftconnection');

    }
    function update_message($appid,$msg)
    {

        $this->db->set('message', $msg);
        $this->db->where("appid",$appid);
        $this->db->update('shiftconnection');
    }
     function forward_report($appid,$result,$cause)
    {

        $this->db->set('result', $result);
        if($result=='Negative')
            $this->db->set('cause', $cause);
        $this->db->where("appid",$appid);
        $this->db->update('shiftconnection');
    }
    function get_status($input)
    {
        $this->db->where($input);
        $query = $this->db->get('shiftconnection');
        if($query->num_rows() > 0)
         {
            $this->db->where('appid',$input['appid']);
            $query = $this->db->get('shiftconnection');
            return $query->row_array();
         }
         else
         {
             $data['appid']=-1;
             return $data;
         }

    }
    function get_appid($phone)
    {
             $query = $this->db->query("SELECT
                    shiftconnection.appid
                    FROM
                    shiftconnection
                    WHERE shiftconnection.phoneno ='$phone'
                    ORDER BY
                    shiftconnection.applydate DESC");
             return $query->result();

    }
}
?>